import {
    request
} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
    data: {
        tabs: [{
                id: 0,
                value: "综合",
                isActive: true
            },
            {
                id: 1,
                value: "销量",
                isActive: false
            },
            {
                id: 2,
                value: "价格",
                isActive: false
            }
        ],
        goodsList: []
    },
    // 请求参数
    QueryParam: {
        keyword: "",
        wid: "",
        currentPage: 1,
        pageSize: 10,
        type: 0
    },
    // 总页数
    totalPages: 1,
    onLoad: function (options) {
        this.QueryParam.wid = options.wid;
        this.getGoodsList();
    },
    // 获取菜品列表数据
    async getGoodsList() {
        const res = await request({
            url: "/goods/query",
            data: this.QueryParam
        });
        // 获取总条数
        const total = res.data.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.QueryParam.pageSize);
        this.setData({
            goodsList: [...this.data.goodsList, ...res.data.data.records]
        })
        // 关闭下拉刷新窗口
        wx.stopPullDownRefresh()
    },
    // 标题点击事件
    handleTabsItemChange(e) {
        // 获取被点击标题的索引
        const {
            index
        } = e.detail;
        console.log(index);
        // 修改原数组
        let {
            tabs
        } = this.data;
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
        // 赋值到 data 中
        this.setData({
            goodsList: [],
            tabs
        })
        this.QueryParam.type = index;
        this.getGoodsList();
    },
    // 页面上滑，滚动条触发事件
    onReachBottom() {
        // 判断是否还有下一页
        if (this.QueryParam.currentPage >= this.totalPages) {
            // 没有下一页
            wx - wx.showToast({
                title: '到底啦',
            })
        } else {
            // 还有下一页
            this.QueryParam.currentPage++;
            this.getGoodsList();
        }
    },
    // 下拉刷新事件
    onPullDownRefresh() {
        // 重置数组
        this.setData({
            goodsList: []
        })
        // 重置页码
        this.QueryParam.currentPage = 1;
        // 发送请求获取数据
        this.getGoodsList()
    }
})